How to create filters
When defining a Scheduled Export in the Export Properties box, it is possible to select a filter that transforms the standard XML export data format into another format suitable for delivery (e.g. CSV). If a filter is not specified, the export files contain XML formatted data. As final delivery formats may vary widely, System Administrators may need to create localised filters. In this section we look at how to add a filter into the Scheduled Exports facility.
Filters, which are programs or scripts, reside under the etc/exports or local/etc/exports directory on the EMu server.
They may be module specific or may be applicable to all modules. Module specific filters are stored under a sub-directory named after the module to which they apply. For example, Parties module specific filters would be located under etc/exports/eparties or local/etc/exports/eparties.
Note: When adding your own filters to the EMu server, they should be placed under local/etc/exports as etc/exports is reserved for use by EMu upgrades.
The name of the file containing the program or script is the label displayed in the Filters drop list when specifying a new Scheduled Export.
When an export is performed the initial data export is in EMu XML format. The data is placed in a temporary file on the EMu server and passed as an argument to the filter specified for the Scheduled Export. The filter receives only one argument, the name of the file containing the exported data.
Several environment variables are set and available to the filter when it executes:
Variable |
Description |
---|---|
EXPORTTITLE |
The title given to the Scheduled Export when it was created. |
EXPORTIRN |
The IRN (Internal Record Number) of the Schedules record used to generate the exported data file. Using EXPORTIRN it is possible to retrieve details from the Schedules record. |
EXPORTTABLE |
The module from which the data was exported. |
EXPORTSTARTDATE |
The value of #STARTDATE# used by the export process. |
EXPORTENDDATE |
The value of #ENDDATE# used by the export process. |
EXPORTRECORDCOUNT |
The number of records exported. |
If the filter is unable to process the exported data, any error messages should be written to STDERR and the export terminated with a non-zero exit status. This notifies EMu that the filter did not complete successfully and an export failure is recorded. Any text written to STDERR is displayed in the Filter Output box for an Exports record in the Exports module.
Once the filter has processed the export data it should print out to STDOUT a list of all the files generated. These files are moved under the logs/exports directory on the EMu server. The file names are displayed in the Export Files table in the Exports record created. If the filter completed successfully, it should return a zero exit status.
The filter may manipulate the export data in any way required. It may email the data (via sendmail), print it (via lpr or lp) or even send it to remote machines (via rsh or ssh). A copy of the data exported should always be attached to the Export record so it can be reviewed at a later date, if required.
In this example filter we convert the export data from XML to CSV (Comma Separated Values) and email the file(s) produced to a mail alias address (csvexport@client.org) on our local mail server. The mail server will then forward the email onto a host of recipients.
We can use texxmlodbc
to convert the export XML to CSV files. The filter is written using the perl scripting language as it provides a nice package for sending multi-part email messages.
The filter is:
#!/usr/bin/perl
use strict;
use warnings 'all';
use MIME::Lite;
#
# First we convert the EMu XML to CSV and save
# the list of files generated.
#
if (! open(FILES, "texxmlodbc --output-encoding=iso-8859-1 '$ARGV[0]' |"))
{
print STDERR "Cannot execute texxmlodbc";
exit(1);
}
my @files = <FILES>;
close(FILES);
#
# Build up the message to send.
#
my $body = <<END;
The attached files were generated by the EMu Scheduled Export facility.
The details of the export are:
Title: $ENV{EXPORTTITLE}
Module: $ENV{EXPORTTABLE}
Start Date: $ENV{EXPORTSTARTDATE}
End Date: $ENV{EXPORTENDDATE}
END
#
# Create an email message to csvexport@client
.org
#
my $msg = MIME::Lite->new(
To => 'csvexport@client
.org',
Subject => 'EMu Scheduled Export CSV Files',
Type => 'multipart/mixed'
);
#
# Add the email text first.
#
$msg->attach(
Type => 'TEXT',
Data => $body
);
#
# Add each file.
#
foreach my $file (@files)
{
#
# Figure out the file mime type and attach it
#
chomp($file);
my $type = ($file =~ /\.ini$/i) ? 'TEXT' : 'text/csv';
$msg->attach(
Type => $type,
Path => $file,
Disposition => 'attachment'
);
}
#
# Send the email.
#
if (! $msg->send())
{
print STDERR 'Cannot send email to csvexport@client
.org';
exit(1);
}
print STDERR 'Email sent to csvexport@client
.org successfully';
#
# Output the file list for the export record.
#
print join("\n", @files);
exit(0);
Notice how the filter outputs the names of the CSV files at the end so they can be attached to the Export record created. Once attached these files may be reviewed at any time.